﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.ReportBusiness;

public partial class Report_BwScrapinfoReportForm : PageBase
{
    int m_PageSize = 50;
    uMESReportBusiness reportBal = new uMESReportBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        uMESMasterPage master = this.Master as uMESMasterPage;
        master.strNavigation = "当前位置：报废单记录报表";
        master.strTitle = "报废单记录报表";
        master.ChangeFrame(true);
        upageTurning.PageIndexChanged += new pageTurning.PageIndexChangedEventHandler(() => { SearchData(upageTurning.CurrentPageIndex.ToString()); });
        WebPanel = WebAsyncRefreshPanel1;
        ClearMessage();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        try {gdResult.Rows.Clear(); SearchData("1"); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    protected void btnReSet_Click(object sender, EventArgs e)
    {
        txtContainerName.Text = "";txtProcessNo.Text = "";txtSpecName.Text = "";gdResult.Rows.Clear();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try { ExportToExcel(); } catch (Exception ex) { DisplayMessage(ex.Message, false); }
    }
    void SearchData(string index)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = index.ToString();
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();
        

        uMESPagingDataDTO result = reportBal.GetStepScrapInfo(para);

        this.gdResult.DataSource = result.DBTable;
        this.gdResult.DataBind();
            //给分页控件赋值，用于分页控件信息显示
            this.upageTurning.TotalRowCount = int.Parse(result.RowCount);
            this.upageTurning.RowCountByPage = m_PageSize;
       
    }

    DataTable SearchAllData( )
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] ="all";
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();


        uMESPagingDataDTO result = reportBal.GetStepScrapInfo(para);

        return result.DBTable;

    }

    void ExportToExcel() {

        // DataTable dt = uMESCommonFunction.GetGridData(gdResult);

        DataTable dt = SearchAllData();
        //取有效列，且重命名列名
        DataView view = dt.DefaultView;

        dt = view.ToTable(false, "ProcessNo", "ContainerName", "ProductDesc", "conqty", "workflowstepname", "Qty", "lossreasonname", "fullname", "submitdate", "reportname", "reportdate",
            "checkname", "checkdate", "ConPlannedStartDate", "ConPlannedCompletionDate");

        dt.Columns["ProcessNo"].ColumnName = "工作令号";
        dt.Columns["ContainerName"].ColumnName = "批次";
        dt.Columns["ProductDesc"].ColumnName = "产品名称";
        dt.Columns["workflowstepname"].ColumnName = "工序";
        dt.Columns["Qty"].ColumnName = "报废数量";
        dt.Columns["ConQty"].ColumnName = "批次数量";
        dt.Columns["lossreasonname"].ColumnName = "报废原因";
        dt.Columns["fullname"].ColumnName = "报废人";
        dt.Columns["submitdate"].ColumnName = "报废时间";
        dt.Columns["reportname"].ColumnName = "报工人";
        dt.Columns["reportdate"].ColumnName = "报工时间";
        dt.Columns["checkname"].ColumnName = "检验人";
        dt.Columns["checkdate"].ColumnName = "检验时间";
        dt.Columns["ConPlannedStartDate"].ColumnName = "计划开始日期";
        dt.Columns["ConPlannedCompletionDate"].ColumnName = "计划完成日期";

        string strPath =Request.MapPath(Request.ApplicationPath)  ;       
      string filePath = ExportHelper.DataTableExportToExcel2(dt, "报废单记录报表", strPath);
        string url = Request.Url.ToString();
       string baseUrl= Regex.Split(url,"/Report")[0];
        string fileUrl = baseUrl + filePath;


        string strScript = "<script>window.open('" + fileUrl + "');</script>";

        ExcuteScript(strScript);
    }
}